<?php
/*
    Lightweight and multiplatform PHP Multithreading Engine
    Copyright (C) 2008  Anton Vedeshin (anton@vedeshin.com, http://anton.vedeshin.com)
	
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

class logger{
	var $types=array(0,'notice','warning','error','stats');
	var $fp;
	var $isdolog;
	function logger($name){
		if(!is_dir("logs")){
			mkdir("logs","0493");
		}
		$this->fp=fopen("logs/log_".$name.".txt", "a+");
		$this->isdolog=true;
	}
	function lw($type,$text){
		if($this->isdolog){
			fwrite($this->fp, "[".$this->types[$type]." :".date("g:i:s A")."]".$text."\r\n");
		}
	}
	function dolog($state){
		if(!$state){
			$this->isdolog=false;
			fclose($this->fp);			
		}
	}
	static function deleteold(){
		logger::RemoveDir('logs',false);
	}
	static function RemoveDir($dir, $DeleteMe) {
		if(!$dh = @opendir($dir)) return;
		while (false !== ($obj = readdir($dh))) {
			if($obj=='.' || $obj=='..') continue;
			if (!@unlink($dir.'/'.$obj)) logger::RemoveDir($dir.'/'.$obj, true);
		}
		closedir($dh);
		if ($DeleteMe){
			@rmdir($dir);
		}
	}

}

function Sleeper($mSec)
{
	//    For dummies like me who spent 5 minutes
	//    wondering why socket_create wasn't defined
	if(!function_exists('socket_create')){
		die("Please enable extension php_sockets.dll");
	}
	//    So the socket is only created once
	static $socket=false;
	if($socket===false){
		$socket=array(socket_create(AF_INET,SOCK_RAW,0));
	}
	$pSock=$socket;
	//    Calc time
	$uSex = $mSec * 1000;
	//    Do the waiting
	socket_select($read=NULL,$write=NULL,$pSock,0,$uSex);
	//    OCD
	return true;
}

function sqlEscape($sql) {
	/* De MagicQuotes */
	$fix_str    = stripslashes($sql);
	$fix_str    = str_replace("'","''",$sql);
	$fix_str    = str_replace("\0","[NULL]",$fix_str);
	return $fix_str;
}
?>